package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.domain.SysMail;
import com.ruoyi.system.service.ISysMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-11-28
 */
@RestController
@RequestMapping("/system/mail")
public class SysMailController extends BaseController
{
    @Resource
    private ISysMailService sysMailService;

    @Resource
    private SysPermissionService permissionService;

    /**
     * 查询【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMail sysMail)
    {
        startPage();
        //查询当前用户角色
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        if (roles != null && roles.size() > 0) {
            if (roles.contains("admin") || roles.contains("boss") ||
                    roles.contains("mail")) {
                sysMail.setUserId(null);
            } else {
                sysMail.setUserId(getUserId());
            }
        } else {
            sysMail.setUserId(getUserId());
        }
        List<SysMail> list = sysMailService.selectSysMailList(sysMail);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysMail sysMail)
    {
        List<SysMail> list = sysMailService.selectSysMailList(sysMail);
        ExcelUtil<SysMail> util = new ExcelUtil<SysMail>(SysMail.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysMailService.selectSysMailById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMail sysMail)
    {
        sysMail.setUserId(getUserId());
        if (StringUtils.isBlank(sysMail.getSendTime())) {
            sysMail.setSendTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                    .format(new Date()));
        }
        return toAjax(sysMailService.insertSysMail(sysMail));
    }

    /**
     * 修改【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMail sysMail)
    {
        return toAjax(sysMailService.updateSysMail(sysMail));
    }

    /**
     * 删除【请填写功能名称】
     */
//    @PreAuthorize("@ss.hasPermi('system:mail:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysMailService.deleteSysMailByIds(ids));
    }
}
